<template>
	<view class="gui-padding gui-margin-top">
		<view class="bg" @touchmove.stop.prevent="moveHanle"></view>
		<view class="bb-box">

			<view class="gui-color-orange gui-h3">发现新版本-V{{gxInfo.version}}</view>

			<view class="bb-nr">
				<text><text class="text-orange" style="padding: 0 10rpx;">●</text> {{gxInfo.content }}</text>
			</view>
			<view class="gx" @click="gx()">
				<!-- <button>{{buttonText}}</button>
				<view>{{progress}}</view> -->
				<gui-progress-button :buttonText="buttonText" borderRadius="50rpx"
					:val="progress"></gui-progress-button>
			</view>
			<view class="bgx text-grey-74" @click="isTrue = false">暂不更新</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: uni.getStorageSync("userInfo"),
				ver: 0,
				isTrue: true,
				gxInfo: {},
				buttonText: "立即更新",
				isreplace: false,
				progress: 0,
			}
		},
		onLoad(opt) {
			console.log(opt.gx);
			this.gxInfo = JSON.parse(opt.gx).data;
			console.log("gxinfo", this.gxInfo);
			this.gx()
		},
		methods: {
			gx() {
				let that = this;

				if (uni.getSystemInfoSync().platform == 'android') {
					that.buttonText = "请稍等...";
					if (that.isreplace) {
						return;
					}
					that.isreplace = true;
					console.log("下载地址：" + that.gxInfo.url);
					const downloadTask = uni.downloadFile({
						url: that.gxInfo.url,
						success: (res) => {
							if (res.statusCode === 200) {
								that.progress = 100;
								that.buttonText = '下载完成,即将重启应用';
								plus.runtime.install(res.tempFilePath, {
									force: true // 强制更新
								}, function() {
									console.log("下载成功")
									that.nativeQuit();
								}, function(error) {
									console.error('安装失败，错误原因：' + error);
									uni.showToast({
										title: '安装失败',
										icon: "error",
										duration: 1500
									});
								})
								setTimeout(res => {
									that.isreplace = false;
								}, 1000)
							}
						},
						complete: () => {}
					})
					downloadTask.onProgressUpdate((resx) => {
						console.log('下载进度' + resx.progress);
						that.progress = resx.progress;
					});
				} else if (uni.getSystemInfoSync().platform == 'ios') {
					plus.runtime.launchApplication({
						action: that.gxInfo.url
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
			},

			nativeQuit: function() {
				let _this = this;
				console.log("nativeQuit");
				uni.showToast({
					title: "准备重新启动！"
				});
				uni.getSystemInfo({
					success: function(e) {
						console.log(e.platform)
						Vue.prototype.StatusBar = e.statusBarHeight;
						Vue.prototype.$phoneInfo = e
						// let system = plus.android.import('java.lang.System')

						if (e.platform == 'android') {
							let main = plus.android.runtimeMainActivity();
							//  热更新之后需要杀死进程重新打开,需要引入安卓system的类来实现
							let system = plus.android.importClass('java.lang.System')
							system.exit(0);
						} else {
							plus.ios.import('UIApplication').sharedApplication().performSelector('exit');
						}
					}
				})
			},
		}
	}
</script>

<style>

</style>